<?php
namespace Home\Controller;

use Think\Controller;

class CommonAction extends Controller
{

    public function __construct()
    {
        parent::__construct();

        if (isset($_COOKIE['user_id'])) {
            // 把cookie复制给session
            $this->_copyCookieToSession();
            $userInfo = M('User')->where("id='" . $_COOKIE['user_id'] . "'")->find();
            if ( ! $userInfo) {
                $this->redirect("Index/index");
            }
            $this->_getOnDuty($_COOKIE['username']);
            $this->session = $userInfo;
        } else {
//            echo json_encode($_SERVER);exit;
            $this->redirect("Index/index", array("redirect" => urlencode($_SERVER['REQUEST_URI'])));
        }
    }

    protected function _copyCookieToSession()
    {
        $_SESSION['user_id'] = $_COOKIE['user_id'];
        $_SESSION['username'] = $_COOKIE['username'];
        $_SESSION['dept'] = $_COOKIE['dept'];
        $_SESSION['dept_name'] = $_COOKIE['dept_name'];
        $_SESSION['role'] = $_COOKIE['role'];
    }

    protected function _getSectionId($class, $function)
    {
        $classPath     = $class;
        $moduleElement = explode('\\', $classPath);
        $className     = end($moduleElement);
        $module        = str_replace('Controller', '', $className);
        $module        = strtolower($module);
        return $module . '/' . $function;
    }

    protected function _getParam($param, $default = '')
    {
        return isset($_REQUEST[$param]) ? $_REQUEST[$param] : $default;
    }

    protected function _getOnDuty($username)
    {
        $weekArray = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
        $sql       = "select * from zt_crm_duty where " . $weekArray[date('w',
                time())] . "=1 and is_delete=0 and name='" . $username . "'";
        $res       = M()->query($sql);

        if ($res) {
            $_SESSION['on_duty'] = true;
        } else {
            $_SESSION['on_duty'] = false;
        }
    }
}